package cn.tianyigps.tianyigpsgatherchecker.customer;

import cn.tianyigps.tianyigpsgatherchecker.service.CheckerService;
import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpskafka.task.customer.Customer;
import cn.tianyigps.tianyigpspublic.util.FormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Optional;

public class GatherCustomer extends Customer {
    @Autowired
    CheckerService checkerService;
    @Override
    public boolean checkTask(Task task) {
        Map<String, Object> params = task.getParams();
        if (!Optional.ofNullable(params).isPresent())
            return false;
        if (StringUtils.isEmpty( FormatUtil.format2String(params.get("equipmentId"))))
            return false;
        if (task.getNextTask() == null)
            return false;
        Task nextTask = task.getNextTask();
        if (StringUtils.isEmpty(nextTask.getTaskType()))
            return false;
        if (!nextTask.getParams().containsKey("callback") || StringUtils.isEmpty(nextTask.getParams().get("callback")))
            return false;


        return true;
    }

    @Override
    public void consume(Task task) {
        checkerService.GatherCheck(FormatUtil.format2String(task.getParams().get("equipmentId")));
    }
}
